package nl.ru.rd.facedetection.nnbfd;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * A JFrame with an image as body.
 * 
 * As to preview an image.
 * 
 * @author Wouter Geraedts (s0814857 - wgeraedts) PGP 66AA5935
 */
public class ImageViewer extends JFrame
{
	private static final long serialVersionUID = 9123587876074401063L;

	/**
	 * Default constructor of an ImageViewer, title will be "Image Preview".
	 * 
	 * @param image
	 *            The image to be shown.
	 */
	public ImageViewer(BufferedImage image)
	{
		super("Image Preview");
		this.initialize(image);
	}

	/**
	 * Second constructor of an ImageViewer, with custom title.
	 * 
	 * @param image
	 *            The image to be shown.
	 * @param title
	 *            The title for the new JFrame.
	 */
	public ImageViewer(BufferedImage image, String title)
	{
		super(title);
		this.initialize(image);
	}

	/**
	 * Shows an Image with a specific list of Windows drawn upon the image.
	 * 
	 * @param image
	 *            The image to be shown.
	 * @param windows
	 *            A list of Windows to be drawn upon the image.
	 */
	public ImageViewer(BufferedImage image, ArrayList<Window> windows)
	{
		super("Image Preview");

		Graphics2D g = image.createGraphics();
		for(Window window : windows)
		{
			double scale = Math.pow(Windows.resizeFactor, window.getScale());
			g.drawRect((int) Math.round(window.getPosX() * scale), (int) Math.round(window.getPosY() * scale), (int) Math.round(Window.size * scale),
					(int) Math.round(Window.size * scale));
		}

		this.initialize(image);
	}

	private void initialize(BufferedImage image)
	{
		JLabel pictureLabel = new JLabel();
		pictureLabel.setIcon(new ImageIcon(image));

		this.add(pictureLabel);
		this.setSize(image.getWidth(), image.getHeight());
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
}